<?php
// mute_check.php

// 确保会话已启动
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

// 当前用户
$currentUser = $_SESSION['username'] ?? null;
if (!$currentUser) return;

// 群聊ID
$groupId = $_GET['id'] ?? null;
if (!$groupId) return;

// 如果是获取消息的请求，直接退出
if (isset($_GET['fetch_messages'])) {
    return;
}

// 禁言配置文件路径
$muteFile = __DIR__ . "/ql/$groupId/jy.json";

// 确保目录存在
$muteDir = dirname($muteFile);
if (!is_dir($muteDir)) {
    mkdir($muteDir, 0777, true);
}

// 确保文件存在
if (!file_exists($muteFile)) {
    file_put_contents($muteFile, json_encode([]));
}

// 读取禁言数据
$muteData = json_decode(file_get_contents($muteFile), true) ?: [];

// 检查当前用户是否被禁言
$isMuted = false;
$muteReason = '';
$muteEndTime = '';

if (isset($muteData[$currentUser])) {
    $muteInfo = $muteData[$currentUser];
    
    // 检查禁言是否过期
    $endTime = mktime(
        $muteInfo['hour'] ?? 0,
        $muteInfo['minute'] ?? 0,
        0,
        $muteInfo['month'] ?? 1,
        $muteInfo['day'] ?? 1,
        $muteInfo['year'] ?? date('Y')
    );
    
    if ($endTime > time()) {
        $isMuted = true;
        $muteReason = $muteInfo['reason'] ?? '违反群规';
        $muteEndTime = sprintf(
            '%04d-%02d-%02d %02d:%02d',
            $muteInfo['year'],
            $muteInfo['month'],
            $muteInfo['day'],
            $muteInfo['hour'],
            $muteInfo['minute']
        );
    } else {
        // 禁言已过期，删除记录
        unset($muteData[$currentUser]);
        file_put_contents($muteFile, json_encode($muteData));
    }
}

// 如果被禁言，显示提示并禁用输入
if ($isMuted) {
    echo <<<HTML
    <style>
    /* 禁言提示样式 */
    #mute-notice {
        position: fixed;
        bottom: 100px;
        left: 0;
        right: 0;
        background: linear-gradient(135deg, #ff6b6b, #ff8e8e);
        color: white;
        padding: 15px;
        text-align: center;
        z-index: 500;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 15px;
        box-shadow: 0 -5px 15px rgba(0,0,0,0.2);
    }
    
    .mute-icon {
        font-size: 28px;
    }
    
    .mute-content {
        text-align: left;
    }
    
    .mute-title {
        font-weight: bold;
        font-size: 18px;
        margin-bottom: 5px;
    }
    
    .mute-detail {
        font-size: 14px;
        opacity: 0.9;
    }
    </style>
    
    <div id="mute-notice">
        <i class="fas fa-microphone-slash mute-icon"></i>
        <div class="mute-content">
            <div class="mute-title">你已被禁言</div>
            <div class="mute-detail">原因: {$muteReason}</div>
            <div class="mute-detail">解禁时间: {$muteEndTime}</div>
        </div>
    </div>
    
    <script>
    // 禁用输入区域
    document.addEventListener('DOMContentLoaded', function() {
        // 禁用发送按钮
        const sendBtn = document.querySelector('.btn-send');
        if (sendBtn) {
            sendBtn.disabled = true;
            sendBtn.style.opacity = '0.5';
            sendBtn.style.cursor = 'not-allowed';
        }
        
        // 禁用表情按钮
        const emojiBtn = document.getElementById('emoji-btn');
        if (emojiBtn) {
            emojiBtn.disabled = true;
            emojiBtn.style.opacity = '0.5';
            emojiBtn.style.cursor = 'not-allowed';
        }
        
        // 禁用图片按钮
        const imageBtn = document.getElementById('image-btn');
        if (imageBtn) {
            imageBtn.disabled = true;
            imageBtn.style.opacity = '0.5';
            imageBtn.style.cursor = 'not-allowed';
        }
        
        // 禁用输入框
        const messageInput = document.getElementById('message-input');
        if (messageInput) {
            messageInput.disabled = true;
            messageInput.placeholder = '你已被禁言，无法发送消息';
            messageInput.style.opacity = '0.7';
            messageInput.style.cursor = 'not-allowed';
        }
        
        // 修改发送函数
        window.sendMessage = function() {
            alert('你已被禁言，无法发送消息');
            return false;
        };
        
        window.sendImage = function(e) {
            alert('你已被禁言，无法发送图片');
            if (e && e.target) {
                e.target.value = '';
            }
            return false;
        };
    });
    </script>
HTML;
}
?>